﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using QLHD.Models;
using System.Globalization;

namespace QLHD.Account
{
    public partial class GroupPermissions : SystemAdmin
    {
        public class Block
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<string> BlockNames { get; set; }

            public Block(int id, string name, params string[] names)
            {
                ID = id;
                Name = name;
                BlockNames = new List<string>();
                foreach (var item in names)
                {
                    BlockNames.Add(item);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (IsPostBack)
                return;

            QLHD2013Entities db = new QLHD2013Entities();

            List<PhongBan> lstPhong = new List<PhongBan>();
            lstPhong = db.PhongBans.Where(p => p.TrangThai == true).ToList();
            List<Block> blocks = new List<Block>();
            //List<PhongBan> pb;
            foreach (PhongBan item in lstPhong)
            {
                blocks.Add(new Block(item.ID, item.Ten, "None", "View", "Edit", "Add"));
            }
            //blocks.Add(new Block(1, "None", "View", "Edit", "Add"));
            //blocks.Add(new Block(2, "None1", "View1", "Edit1", "Add1"));
            //blocks.Add(new Block(3, "None3", "View3", "Edit3", "Add3"));
            rptDummy.DataSource = blocks;
            rptDummy.DataBind();
        }

        protected void rptDummy_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //Block blk = (Block)e.Item.DataItem;
                //Repeater sub = (Repeater)e.Item.FindControl("rptCols");

                //RadioButtonList list = new RadioButtonList();
                //list.RepeatDirection = RepeatDirection.Horizontal;
                //list.ID = "rblBlocks";
                //list.Attributes.Add("BlockID", blk.ID.ToString());

                //foreach (string item in blk.BlockNames)
                //{
                //    list.Items.Add(new ListItem(item, item));
                //}
                ////sub.DataSource = list;
                ////sub.DataBind();
                //e.Item.Controls.Add(list);
                QLHD2013Entities db = new QLHD2013Entities();
                //PermissionsUser objPU = new PermissionsUser();


                int permissionvalue = 3;
                int groupID = int.Parse(Request.QueryString["GroupID"].ToString());
                HiddenField hidInput = (HiddenField)e.Item.FindControl("ID");
                int nitemID = int.Parse(hidInput.Value);
                var perValue = db.PermissionsUsers.Where(q => q.GroupID == groupID && q.ItemID == nitemID &&
                     q.ObjectID == 1).Select(q => q.PermissionValue).FirstOrDefault();
                RadioButtonList rdlst = (RadioButtonList)e.Item.FindControl("rdlist");
                if (rdlst != null)
                {
                    rdlst.SelectedValue = perValue.ToString();
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            QLHD2013Entities db = new QLHD2013Entities();
            int groupID = int.Parse(Request.QueryString["GroupID"].ToString());
            List<GroupUser> objGU = db.GroupUsers.Where(p => p.GroupID == groupID).ToList();
            foreach (GroupUser itemGU in objGU)
            {
                foreach (RepeaterItem item in rptDummy.Items)
                {
                    var rdbList = item.FindControl("rdlist") as RadioButtonList;
                    int permision = int.Parse(rdbList.SelectedValue);
                    int ObjectID = 1;
                    var idInput = item.FindControl("ID") as HiddenField;
                    int nitemID = int.Parse(idInput.Value);

                    PermissionsUser objPU = db.PermissionsUsers.Where(q => q.GroupID == groupID && q.ItemID == nitemID &&
                    q.ObjectID == ObjectID && q.UserID == itemGU.UserID).FirstOrDefault();

                    if (objPU != null)
                    {
                        objPU.ObjectID = ObjectID;
                        objPU.UserID = itemGU.UserID;
                        objPU.GroupID = groupID;
                        objPU.ItemID = nitemID;
                        objPU.PermissionValue = permision;
                    }
                    else
                    {
                        objPU = new PermissionsUser();
                        objPU.ObjectID = ObjectID;
                        objPU.UserID = itemGU.UserID;
                        objPU.GroupID = groupID;
                        objPU.ItemID = nitemID;
                        objPU.PermissionValue = permision;
                        db.AddToPermissionsUsers(objPU);
                    }
                    db.SaveChanges();
                }
            }
            Response.Redirect("GroupPermissionList.aspx");
        }
    }
}